package com.hyfd.task;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.bonc.vip.bj.md5.VIPMD5;
import com.hyfd.common.utils.MD5;
import com.hyfd.common.utils.ToolHttp;
import com.hyfd.common.utils.XmlUtils;
import com.hyfd.dao.mp.OrderDao;
import com.hyfd.dao.mp.ProviderPhysicalChannelDao;
import com.hyfd.rabbitMq.RabbitMqProducer;
import com.hyfd.rabbitMq.SerializeUtil;

@Component
public class BeijingUnicomFlowTask {

Logger log = Logger.getLogger(getClass());
	
	@Autowired
	OrderDao orderDao;
	@Autowired
	RabbitMqProducer mqProducer;//消息队列生产者
	@Autowired
	ProviderPhysicalChannelDao providerPhysicalChannelDao;//物理通道信息
	
//	@Scheduled(fixedDelay = 60000)
	public void queryBeijingUnicomOrder(){
		Map<String,Object> map = new HashMap<String,Object>();
		int flag = 2;
		try {
			String providerId = "0000000058";//
			Map<String,Object> channel = providerPhysicalChannelDao.selectByProviderId(providerId);//获取通道
			String defaultParameter = (String) channel.get("default_parameter");//参数
			Map<String,String> paramMap = XmlUtils.readXmlToMap(defaultParameter);
			//渠道工号
			String gonghao = paramMap.get("gonghao");
			String privateKey = paramMap.get("privateKey");//平台的appKey
			// 接口服务器地址
			String host = paramMap.get("Host");
			// 接口服务端口
			String port = paramMap.get("Port");
			//设备号码类型
			String phNumType = "G";
			//查询类型
			String queryType="P";
			//查询订单中属于北京联通的折扣
			Map<String,Object> param = new HashMap<String,Object>();
			param.put("dispatcherProviderId", providerId);
			param.put("status", "1");
			List<Map<String,Object>> orderList = orderDao.selectByTask(param);//查询出所有北京联通的充值中的订单
			for(int i = 0 , size = orderList.size() ; i < size ; i ++){
				Map<String,Object> order = orderList.get(i);
				String orderId = order.get("orderId")+"";//平台订单号
				String mobile = (String) order.get("phone");
				map.put("orderId", orderId);
				//组装url
//				String linkUrl = "http://"+host+":"+port+"/ua/infoSearch";
				String md5Str = privateKey + mobile + phNumType + gonghao + queryType;
				md5Str =VIPMD5.encode(md5Str);
				String url = "http://" + host + ":" + port + "/ua/infoSearch/t/" + mobile + "/P/" + phNumType + "/u/" + gonghao+ "/o/"+queryType + "/m/" + md5Str;
				String result = ToolHttp.post(false, url, "", null);
//				String paramStr = "t="+mobile+"&p=G"+"&u="+gonghao+"&o=P"+"&m="+md5Str;
//				String result = ToolHttp.post(false, linkUrl, paramStr, "application/x-www-form-urlencoded");
				//判断是否成功
				if(result!=null && !result.equals("")){
					JSONObject resultJson = JSONObject.parseObject(result);//将返回值转为json对象
					JSONObject resultJ = resultJson.getJSONObject("result");
					String code = resultJ.getString("code");
					String message=resultJ.getString("message");
					//code=1为成功
					if ("1".equals(code)) {
						flag = 1;
					}else if("0".equals(code)){
						flag = 0;//失败
					}else{
						flag = -1;
					}
					map.put("resultCode", code + ":" + message);
				}
				map.put("status", flag);
				mqProducer.sendDataToQueue(RabbitMqProducer.Result_QueueKey, SerializeUtil.getStrFromObj(map));
			}
		} catch (Exception e) {
			log.error("北京联通流量查询出错"+e);
		}
	}
}
